package com.biz.primus.common.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Spring容器工具类
 *
 * @author david-liu
 */
@Component
public class SpringContextUtils implements ApplicationContextAware {

    private static ApplicationContext APPLICATION_CONTEXT;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        APPLICATION_CONTEXT = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return APPLICATION_CONTEXT;
    }

    public static <T> T getBean(Class<T> beanType) {
        return APPLICATION_CONTEXT == null ? null : APPLICATION_CONTEXT.getBean(beanType);
    }

    public static Object getBean(String beanName) {
        return APPLICATION_CONTEXT == null ? null : APPLICATION_CONTEXT.getBean(beanName);
    }
}
